﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;


namespace HdipCricketClub.Controllers
{
    public class SelectTeamController : Controller
    {
        // database context

        private CricketClubEntities db = new CricketClubEntities();

        // database query repositories

        FixtureRepository fixtureRepository = new FixtureRepository();
        MemberRepository memberRepository = new MemberRepository();
        ClubFixturePlayerRepository clubFixturePlayerRepository = new ClubFixturePlayerRepository();

        // objects

        SelectPlayersViewModel selectPlayersViewModel = new SelectPlayersViewModel();
        SelectedPlayersViewModel selectedPlayersViewModel = new SelectedPlayersViewModel();
        SelectedTeamCreatedViewModel selectedTeamCreatedViewModel = new SelectedTeamCreatedViewModel();
        ClubFixturePlayer clubFixturePlayer = new ClubFixturePlayer();

        //
        //  GET: /SelectTeam/SelectFixture/

        public ViewResult SelectFixture()
        {
            var fixtures = fixtureRepository.FindFixturesWithNoTeamSelected();

            if (fixtures.Count() == 0)
            {
                ViewBag.Message = "There are no fixtures where a team has not been selected";
            }

            return View(fixtures.ToList());
        }

        //
        //  GET:  /SelectTeam/SelectPlayers/

        public ActionResult SelectPlayers(int id)
        {
            SelectPlayersViewModel selectPlayers = new SelectPlayersViewModel();
            selectPlayers.SelectedFixture = fixtureRepository.GetFixture(id);
            selectPlayers.AvailablePlayers = memberRepository.FindAllPlayers();

            return View(selectPlayers);
        }

        //
        //  POST:  /SelectTeam/SelectPlayers/

        [HttpPost]
        public ActionResult SelectPlayers(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                SelectedPlayersViewModel selectedTeam = selectedPlayersViewModel.ParseFormCollection(formCollection);

                if (selectedTeam.noOfPlayersSelected == selectedTeam.noOfPlayersRequired)
                {
                    // Save selected team

                    SelectedTeamCreatedViewModel fixtureTeam = new SelectedTeamCreatedViewModel();
                    fixtureTeam.TeamCreated = clubFixturePlayer.CreateTeamForFixture(selectedTeam.PlayersSelected, selectedTeam.SelectedFixture);
                    fixtureTeam.SelectedFixture = selectedTeam.SelectedFixture;

                    // return confirmation view

                    return View("ConfirmTeam", fixtureTeam);

                }
                else
                {
                    SelectedPlayersViewModel teamToEdit = new SelectedPlayersViewModel();
                    teamToEdit.SelectedFixture = fixtureRepository.GetFixture(selectedTeam.SelectedFixture.fixture_id);
                    teamToEdit.PlayersSelected = memberRepository.GetPlayersSelectedDetails(selectedTeam.PlayersSelected);
                    teamToEdit.PlayersNotSelected = memberRepository.GetPlayersNotSelectedDetails(selectedTeam.PlayersNotSelected);
                    teamToEdit.noOfPlayersSelected = teamToEdit.PlayersSelected.Count();
                    teamToEdit.noOfPlayersRequired = teamToEdit.SelectedFixture.no_of_players;

                    if (teamToEdit.noOfPlayersSelected > teamToEdit.noOfPlayersRequired)
                    {
                        ViewBag.Message = "You have selected " + teamToEdit.noOfPlayersSelected + " players, please 'unselect' " + (teamToEdit.noOfPlayersSelected - teamToEdit.noOfPlayersRequired) + " players.";
                    }
                    if (teamToEdit.noOfPlayersRequired > teamToEdit.noOfPlayersSelected)
                    {
                        ViewBag.Message = "You have selected " + teamToEdit.noOfPlayersSelected + " players, please select a further " + (teamToEdit.noOfPlayersRequired - teamToEdit.noOfPlayersSelected) + " players.";
                    }
                    return View("ReInputTeam", teamToEdit);
                }
            }
            return View();
        }

        //
        //  GET:    /SelectTeam/ContactPlayers/

        public ActionResult ContactPlayers(int id)
        {
            SelectedTeamCreatedViewModel fixtureTeam = new SelectedTeamCreatedViewModel();
            fixtureTeam.TeamCreated = clubFixturePlayerRepository.GetClubFixturePlayers(id);
            fixtureTeam.SelectedFixture = fixtureRepository.GetFixture(id);

            return View(fixtureTeam);
        }

        //
        //  POST:   /SelectTeam/ContactPlayers/

        [HttpPost]
        public ActionResult ContactPlayers(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                SelectedTeamCreatedViewModel fixtureTeam = selectedTeamCreatedViewModel.ParseFormCollection(formCollection);

                return View("PlayersContacted", fixtureTeam);
            }
            return View();
        }

        //
        //  GET:   /SelectTeam/ContactList/

        public ActionResult ContactList()
        {
            // Get fixtures where a team exists but no scorecard has been entered

            IQueryable<ScorecardHeader> fixturesWithScorecardEntered = db.ScorecardHeaders.Where(sc => sc.scorecard_entered == true);

            List<int> fwse = fixturesWithScorecardEntered.Select(f => f.fixture_id).ToList();

            // Get fixtures with a team selected

            List<int> fixturesWithTeamSelected = db.ClubFixturePlayers.Select(cfp => cfp.fixture_id).Distinct().ToList();

            List<Fixture> allFixtures = fixtureRepository.FindAllFixtures().ToList();

            List<Fixture> FixturesWithTeamAndNoScorecard = new List<Fixture>();

            foreach (Fixture fixture in allFixtures)
            {
                if ((!fwse.Contains(fixture.fixture_id)) && (fixturesWithTeamSelected.Contains(fixture.fixture_id)))
                {
                    FixturesWithTeamAndNoScorecard.Add(fixture);
                }
            }
            return View(FixturesWithTeamAndNoScorecard);
        }

        //
        //  GET /SelectTeam/ContactSelectedPlayers/

        public ActionResult ContactSelectedPlayers(int id)
        {
            SelectedTeamCreatedViewModel teamPlayers = new SelectedTeamCreatedViewModel();

            teamPlayers.SelectedFixture = fixtureRepository.GetFixture(id);
            teamPlayers.TeamCreated = clubFixturePlayerRepository.GetClubFixturePlayers(id);

            return View(teamPlayers);
        }

        //
        //  POST  /SelectTeam/ContactSelectedPlayers/

        [HttpPost]
        public ActionResult ContactSelectedPlayers(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                SelectedTeamCreatedViewModel teamPlayers = selectedTeamCreatedViewModel.ParseFormCollection(formCollection);

                return View("ContactPlayersContacted", teamPlayers);
            }
            return View();
        }

        //
        //  GET   /SelectTeam/EditList/

        public ActionResult EditList()
        {
            // Get fixtures where a team exists but no scorecard has been entered

            IQueryable<ScorecardHeader> fixturesWithScorecardEntered = db.ScorecardHeaders.Where(sc => sc.scorecard_entered == true);

            List<int> fwse = fixturesWithScorecardEntered.Select(f => f.fixture_id).ToList();

            // Get fixtures with a team selected

            List<int> fixturesWithTeamSelected = db.ClubFixturePlayers.Select(cfp => cfp.fixture_id).Distinct().ToList();

            List<Fixture> allFixtures = fixtureRepository.FindAllFixtures().ToList();

            List<Fixture> FixturesWithTeamAndNoScorecard = new List<Fixture>();

            foreach (Fixture fixture in allFixtures)
            {
                if ((!fwse.Contains(fixture.fixture_id)) && (fixturesWithTeamSelected.Contains(fixture.fixture_id)))
                {
                    FixturesWithTeamAndNoScorecard.Add(fixture);
                }
            }
            return View(FixturesWithTeamAndNoScorecard);
        }

        //
        //  GET:    SelectTeam/EditTeam/

        public ActionResult EditTeam(int id)
        {
            // get current fixture team

            List<ClubFixturePlayer> currentSelection = clubFixturePlayerRepository.GetClubFixturePlayers(id);

            // get members in current team and members available for selection

            SelectedPlayersViewModel currentTeam = memberRepository.GetPlayerLists(currentSelection, id);

            return View(currentTeam);
        }

        //
        //  POST:  /SelectTeam/EditTeam/

        [HttpPost]
        public ActionResult EditTeam(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {   
                SelectedPlayersViewModel selectedTeam = selectedPlayersViewModel.ParseFormCollection(formCollection);
                
                if (selectedTeam.noOfPlayersSelected == selectedTeam.noOfPlayersRequired)
                {
                    // Update selected team

                    SelectedTeamCreatedViewModel editedTeam = new SelectedTeamCreatedViewModel();
                    editedTeam.TeamCreated = clubFixturePlayer.EditTeamForFixture(selectedTeam.PlayersSelected, selectedTeam.SelectedFixture);
                    editedTeam.SelectedFixture = selectedTeam.SelectedFixture;

                    // return confirmation view

                    return View("ConfirmEditedTeam", editedTeam);

                }
                else
                {
                    SelectedPlayersViewModel teamToEdit = new SelectedPlayersViewModel();
                    teamToEdit.SelectedFixture = fixtureRepository.GetFixture(selectedTeam.SelectedFixture.fixture_id);
                    teamToEdit.PlayersSelected = memberRepository.GetPlayersSelectedDetails(selectedTeam.PlayersSelected);
                    teamToEdit.PlayersNotSelected = memberRepository.GetPlayersNotSelectedDetails(selectedTeam.PlayersNotSelected);
                    teamToEdit.noOfPlayersSelected = teamToEdit.PlayersSelected.Count();
                    teamToEdit.noOfPlayersRequired = teamToEdit.SelectedFixture.no_of_players;

                    if (teamToEdit.noOfPlayersSelected > teamToEdit.noOfPlayersRequired)
                    {
                        ViewBag.Message = "You have selected " + teamToEdit.noOfPlayersSelected + " players, please 'unselect' " + (teamToEdit.noOfPlayersSelected - teamToEdit.noOfPlayersRequired) + " players.";
                    }
                    if (teamToEdit.noOfPlayersRequired > teamToEdit.noOfPlayersSelected)
                    {
                        ViewBag.Message = "You have selected " + teamToEdit.noOfPlayersSelected + " players, please select a further " + (teamToEdit.noOfPlayersRequired - teamToEdit.noOfPlayersSelected) + " players.";
                    }
                    return View("ReInputEditTeam", teamToEdit);
                }
            }
            return View();

        }
    }
}